import thin_render from "../../../lib/thin_render.mjs";
import render_helper from "../../../lib/render_helper.mjs";
import { renderNumber } from "../formatters/number.mjs";
import { renderDateTime } from "../formatters/datetime.mjs";
import { renderAjaxContent } from "../formatters/ajax.mjs";
import { saveScrollPosition } from "../../utils/scroll-position.mjs";

/**
 * 渲染普通单元格
 * @param {Object} params - 渲染参数
 */
export function renderNormalCell({ td, col, tr, template }) {
  // console.log({ td, col, tr, template });

  if (col.click) render_helper(td, { click: col.click });

  if (/\[\[[\w/.]+\]\]/.test(col.col)) {
    // console.log(0);
    thin_render(td, col.col);
  } else if (typeof col.col === "object" || typeof col.col === "function") {
    // console.log(1);
    thin_render(td, col.col);
  } else if (col.NumberFormat) {
    // console.log(2);
    renderNumber(td, col);
    bindRowClick();
  } else if (col.DateTimeFormat) {
    // console.log(3);
    renderDateTime(td, col);
    bindRowClick();
  } else if (typeof col.col === "string") {
    // console.log(4);
    renderAjaxContent(td, col, template);
    bindRowClick();
  }

  function bindRowClick() {
    // console.log({ template });
    if (template.rowclick && !col.click) {
      render_helper(td, {
        click: (e) => {
          let gridview = tr.closest("gridview");
          saveScrollPosition(gridview, template);
          template.rowclick(e);
        },
      });
    }
  }
}
